package com.dbgs.executer.wrapper;

import com.dbgs.executer.callback.NormalCallback;
import com.dbgs.executer.callback.ThreadCallback;
import com.dbgs.executer.config.ThreadConfigs;
import com.dbgs.executer.utils.ThreadToolUtils;

import java.util.concurrent.Callable;

public final class CallableWrapper<T> implements Callable<T> {
    private String name;
    private ThreadCallback callback;
    private Callable<T> proxy;

    /**
     * 构造方法
     * @param configs               thread配置，主要参数有：线程name，延迟time，回调callback，异步callback
     * @param proxy                 线程优先级
     */
    public CallableWrapper(ThreadConfigs configs, Callable<T> proxy) {
        this.name = configs.name;
        this.proxy = proxy;
        this.callback = new NormalCallback(configs.callback, configs.deliver, configs.asyncCallback);
    }


    @Override
    public T call() throws Exception {
        ThreadToolUtils.resetThread(Thread.currentThread(),name,callback);
        if (callback != null) {
            //开始
            callback.onStart(name);
        }
        T t = null;
        try {
            t = proxy == null ? null : proxy.call();
        } catch (Exception e) {
            e.printStackTrace();
            //异常错误
            if(callback!=null){
                callback.onError(name,e);
            }
        }finally {
            //完成
            if (callback != null)  {
                callback.onCompleted(name);
            }
        }
        return t;
    }
}
